/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PDFMergerUI.java
 *
 * Created on Jan 12, 2011, 6:02:33 AM
 */
package project.jpdftools.pdfmerger;

import java.awt.Toolkit;
import java.io.File;
import java.util.logging.Level;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import project.jpdftools.animation.AnimationWorker;
import project.jpdftools.constants.Const.MOVE_ITEM;
import project.jpdftools.managers.LocalizationManager;
import project.jpdftools.managers.LoggerManager;
import project.jpdftools.managers.PDFManager;
import project.jpdftools.other.IntegerTextField;
import project.jpdftools.other.KeyValue;
import project.jpdftools.pdfmerger.actionlisteners.TableUpDownAction;
import project.jpdftools.pdfmerger.thread.PDFMergerThread;

/**
 *
 * @author Mido
 */
public class PDFMergerUI extends JFrame {

    private static boolean is_component_visible = false;
    private DefaultTableCellRenderer table_cell_renderer = new DefaultTableCellRenderer();

    /**
     * Creates new form PDFMergerUI
     */
    public PDFMergerUI() {
        // set cell renderer alignment
        table_cell_renderer.setHorizontalAlignment(JLabel.CENTER);
        // intialize all of the components
        initComponents();
        // center the window
        project.jpdftools.toolkit.Toolkit.centerWindow(this, this.getHeight(), this.getWidth());

        // add action listeners
        bt_move_up.addActionListener(new TableUpDownAction(t_merge_files, MOVE_ITEM.move_up));
        bt_move_down.addActionListener(new TableUpDownAction(t_merge_files, MOVE_ITEM.move_down));

        // indicate that the frame is visible
        is_component_visible = true;
        LoggerManager.getLogger().log(Level.INFO, "Frame shown \"{0}\".", "PDFMergerUI");
    }

    public static boolean IsComponentVisible() {
        return is_component_visible;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bt_merge = new javax.swing.JButton();
        bt_cancel = new javax.swing.JButton();
        p_note = new javax.swing.JPanel();
        ta_info = new javax.swing.JTextArea();
        bt_info = new javax.swing.JButton();
        p_files_to_merge = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        t_merge_files = new javax.swing.JTable();
        bt_add_and_browse = new javax.swing.JButton();
        bt_remove = new javax.swing.JButton();
        bt_move_up = new javax.swing.JButton();
        bt_move_down = new javax.swing.JButton();
        bt_remove_all = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        tf_title = new javax.swing.JTextField();
        l_subject = new javax.swing.JLabel();
        l_keywords = new javax.swing.JLabel();
        l_author = new javax.swing.JLabel();
        tf_subject = new javax.swing.JTextField();
        tf_author = new javax.swing.JTextField();
        l_title = new javax.swing.JLabel();
        tf_keywords = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(LocalizationManager.getCurrent_resource().getString("MAIN_MENU_MERGE_FILES"));
        setIconImage(Toolkit.getDefaultToolkit().getImage("Data/Icons/Soft Scraps Icons/10.png"));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                PDFMergerUI.this.windowClosing(evt);
            }
        });

        bt_merge.setText(LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_MERGE"));
        bt_merge.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_mergeActionPerformed(evt);
            }
        });

        bt_cancel.setText(LocalizationManager.getCurrent_resource().getString("CANCEL"));
        bt_cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cancelActionPerformed(evt);
            }
        });

        p_note.setBorder(javax.swing.BorderFactory.createTitledBorder(null, LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_NOTE_PANEL_TITLE"), javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Times New Roman", 1, 12), new java.awt.Color(0, 0, 255))); // NOI18N

        ta_info.setEditable(false);
        ta_info.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
        ta_info.setColumns(20);
        ta_info.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        ta_info.setLineWrap(true);
        ta_info.setRows(5);
        ta_info.setText(LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_INFO"));
        ta_info.setAutoscrolls(false);
        ta_info.setOpaque(false);
        ta_info.setRequestFocusEnabled(false);

        bt_info.setBorder(null);
        bt_info.setBorderPainted(false);
        bt_info.setContentAreaFilled(false);
        bt_info.setDefaultCapable(false);
        bt_info.setFocusPainted(false);
        bt_info.setFocusable(false);
        bt_info.setRequestFocusEnabled(false);
        bt_info.setRolloverEnabled(false);

        javax.swing.GroupLayout p_noteLayout = new javax.swing.GroupLayout(p_note);
        p_note.setLayout(p_noteLayout);
        p_noteLayout.setHorizontalGroup(
            p_noteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p_noteLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(bt_info, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ta_info)
                .addContainerGap())
        );
        p_noteLayout.setVerticalGroup(
            p_noteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p_noteLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(p_noteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(ta_info, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(bt_info, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        bt_info.setIcon(new ImageIcon("Data/Icons/Basic/Information-icon.png"));

        p_files_to_merge.setBorder(javax.swing.BorderFactory.createTitledBorder(null, LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_FILES_TO_MERGE"), javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Times New Roman", 1, 12), new java.awt.Color(0, 0, 255))); // NOI18N

        t_merge_files.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "java.lang.Object@11a965e", "java.lang.Object@11a965e", "java.lang.Object@11a965e", "java.lang.Object@11a965e"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        t_merge_files.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(t_merge_files);
        t_merge_files.getColumnModel().getColumn(0).setHeaderValue(LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_FILE_NAME"));
        t_merge_files.getColumnModel().getColumn(1).setHeaderValue(LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_TOTAL_PAGES"));
        t_merge_files.getColumnModel().getColumn(1).setCellRenderer(table_cell_renderer);
        t_merge_files.getColumnModel().getColumn(2).setHeaderValue(LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_FROM_PAGE"));
        t_merge_files.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(new IntegerTextField()));
        t_merge_files.getColumnModel().getColumn(2).setCellRenderer(table_cell_renderer);
        t_merge_files.getColumnModel().getColumn(3).setHeaderValue(LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_TO_PAGE"));
        t_merge_files.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(new IntegerTextField()));
        t_merge_files.getColumnModel().getColumn(3).setCellRenderer(table_cell_renderer);
        t_merge_files.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        javax.swing.GroupLayout p_files_to_mergeLayout = new javax.swing.GroupLayout(p_files_to_merge);
        p_files_to_merge.setLayout(p_files_to_mergeLayout);
        p_files_to_mergeLayout.setHorizontalGroup(
            p_files_to_mergeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p_files_to_mergeLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE)
                .addContainerGap())
        );
        p_files_to_mergeLayout.setVerticalGroup(
            p_files_to_mergeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p_files_to_mergeLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bt_add_and_browse.setIcon(new ImageIcon("Data/Icons/Basic/Document-Add-icon.png"));
        bt_add_and_browse.setText(LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_ADD_FILE"));
        bt_add_and_browse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_add_and_browseActionPerformed(evt);
            }
        });

        bt_remove.setIcon(new ImageIcon("Data/Icons/Basic/page-remove-icon.png"));
        bt_remove.setText(LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_REMOVE"));
        bt_remove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_removeActionPerformed(evt);
            }
        });

        bt_move_up.setIcon(new ImageIcon("Data/Icons/Basic/Button-Up-icon.png"));
        bt_move_up.setText(LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_MOVE_UP"));

        bt_move_down.setIcon(new ImageIcon("Data/Icons/Basic/Button-Down-icon.png"));
        bt_move_down.setText(LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_MOVE_DOWN"));

        bt_remove_all.setIcon(new ImageIcon("Data/Icons/Basic/remove-icon.png"));
        bt_remove_all.setText(LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_REMOVE_ALL"));
        bt_remove_all.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_remove_allActionPerformed(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, LocalizationManager.getCurrent_resource().getString("MAIN_MENU_DOCUMENT_INFO_TITLE"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 1, 12), new java.awt.Color(0, 0, 255))); // NOI18N

        l_subject.setText("Subject:");

        l_keywords.setText("Keywords:");

        l_author.setText("Author:");

        l_title.setText("Title:");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(l_title)
                    .addComponent(l_subject))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tf_title, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_subject, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(l_author)
                    .addComponent(l_keywords))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tf_author)
                    .addComponent(tf_keywords))
                .addGap(117, 117, 117))
        );

        jPanel4Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {tf_subject, tf_title});

        jPanel4Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {tf_author, tf_keywords});

        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(l_title)
                            .addComponent(tf_author, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(l_author)
                            .addComponent(tf_title, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                                    .addComponent(tf_subject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(l_subject)))
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addGap(6, 6, 6)
                                .addComponent(tf_keywords, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.CENTER, jPanel4Layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(l_keywords)))
                .addContainerGap())
        );

        jPanel4Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {tf_subject, tf_title});

        jPanel4Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {tf_author, tf_keywords});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(p_files_to_merge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(bt_move_down, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(bt_move_up, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(bt_remove_all, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(bt_remove, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(bt_add_and_browse, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(p_note, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(bt_merge)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_cancel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(p_note, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addComponent(bt_add_and_browse)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_remove)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_remove_all)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_move_up)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_move_down))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(p_files_to_merge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_cancel)
                    .addComponent(bt_merge))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt_cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelActionPerformed
        // close the frame
        this.dispose();
        // indicate that the frame is hidden
        is_component_visible = false;
        LoggerManager.getLogger().log(Level.INFO, "Frame hidden \"{0}\".", "PDFMergerUI");
    }//GEN-LAST:event_bt_cancelActionPerformed

    private void windowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_windowClosing
        bt_cancelActionPerformed(null);
    }//GEN-LAST:event_windowClosing

    private void bt_add_and_browseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_add_and_browseActionPerformed
        // choose file location
        JFileChooser file_chooser = new JFileChooser();

        file_chooser.setDialogTitle(LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_FILE_CHOOSER_TITLE"));
        file_chooser.setMultiSelectionEnabled(true);
        file_chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        file_chooser.setFileFilter(new FileNameExtensionFilter("Pdf files (*.pdf)", "pdf"));

        int returnVal = file_chooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File[] selected_files = file_chooser.getSelectedFiles();
            for (int i = 0; i < selected_files.length; i++) {
                if (PDFManager.isPdf(selected_files[i].getAbsolutePath()) == true) {
                    // get number of page in the pdf file
                    int number_of_pages = PDFManager.getNumberOfPages(selected_files[i].getAbsolutePath());
                    // add the file to the model
                    // without specifying the bounds, the whole pages would be used
                    ((DefaultTableModel) t_merge_files.getModel()).addRow(new Object[]{new KeyValue(selected_files[i].getAbsolutePath(),
                                selected_files[i].getName()), number_of_pages, 1, number_of_pages});
                }
            }
        }
    }//GEN-LAST:event_bt_add_and_browseActionPerformed

    private void bt_removeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_removeActionPerformed
        // make sure that there is a file selected
        if (t_merge_files.getSelectedRow() == -1) {
            // show an error message
            JOptionPane.showMessageDialog(null, LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_NO_FILE_SELECTED"),
                    LocalizationManager.getCurrent_resource().getString("ERROR"), 0);
            return;
        }

        // remove the selected file from the list
        ((DefaultTableModel) t_merge_files.getModel()).removeRow(t_merge_files.getSelectedRow());
    }//GEN-LAST:event_bt_removeActionPerformed

    private void bt_mergeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_mergeActionPerformed
        int list_size = t_merge_files.getRowCount();
        // check if there is no files to merge
        if (list_size == 0) {
            // show error message
            JOptionPane.showMessageDialog(null, LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_NO_FILES"),
                    LocalizationManager.getCurrent_resource().getString("ERROR"), 0);
            return;
        }

        // stop editing all the cells and save the data
        if (t_merge_files.isEditing()) {
            t_merge_files.getCellEditor().stopCellEditing();
        }

        // check for data validity before merging
        if (checkValidity() == false) {
            return;
        }

        try {
            // Resulting pdf
            JFileChooser file_chooser = new JFileChooser();
            if (file_chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
                // create a thread to merge the pdf files
                PDFMergerThread thread = new PDFMergerThread(new AnimationWorker(LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_MERGING_PDF_FILES")), t_merge_files, tf_title.getText(),
                        tf_author.getText(), tf_subject.getText(), tf_keywords.getText(), file_chooser.getSelectedFile());
                // start the thread
                thread.start();
            }
        } catch (Exception ex) {
            // show error message
            JOptionPane.showMessageDialog(null, LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_FAIL"),
                    LocalizationManager.getCurrent_resource().getString("ERROR"), 0);
            LoggerManager.getLogger().log(Level.SEVERE, "Merging files failed. Message = {0}", project.jpdftools.toolkit.Toolkit.getStackTraceAsString(ex.getCause()));
        }
    }//GEN-LAST:event_bt_mergeActionPerformed

    private void bt_remove_allActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_remove_allActionPerformed
        // make sure that there are some files inserted
        if (t_merge_files.getRowCount() == 0) {
            // show an error message
            JOptionPane.showMessageDialog(null, LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_NO_FILES_TO_REMOVE"),
                    LocalizationManager.getCurrent_resource().getString("ERROR"), 0);
            return;
        }

        // remove all files from the list
        while (t_merge_files.getRowCount() != 0) {
            ((DefaultTableModel) t_merge_files.getModel()).removeRow(0);
        }
    }//GEN-LAST:event_bt_remove_allActionPerformed

    /**
     * Check data inside the table to see if it's valid or not
     *
     * @return
     */
    private boolean checkValidity() {
        boolean integer_check = false;
        boolean range_check = false;
        boolean bigger_than_total_check = false;
        int row_count = t_merge_files.getRowCount();

        // integer values check
        for (int i = 0; i < row_count; i++) {
            try {
                int data_2 = Integer.valueOf(t_merge_files.getValueAt(i, 2).toString());
                int data_3 = Integer.valueOf(t_merge_files.getValueAt(i, 3).toString());
                // check for zero values
                if (data_2 == 0 || data_3 == 0) {
                    JOptionPane.showMessageDialog(null, LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_FOUND_ZERO")
                            + " " + (i + 1), LocalizationManager.getCurrent_resource().getString("ERROR"), 0);
                    LoggerManager.getLogger().log(Level.SEVERE, "Found zero value at row {0}", (i + 1));
                    integer_check = false;
                    break;
                }
                integer_check = true;
            } catch (Exception ex) {
                // empty lines or using chars
                JOptionPane.showMessageDialog(null, LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_INVALID_DATA")
                        + " " + (i + 1) + "\n(" + LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_CHECK_VALUE") + ")",
                        LocalizationManager.getCurrent_resource().getString("ERROR"), 0);
                LoggerManager.getLogger().log(Level.SEVERE, "Invalid data in merge table. Message = {0}", project.jpdftools.toolkit.Toolkit.getStackTraceAsString(ex.getCause()));
                integer_check = false;
                break;
            }
        }
        // range check
        if (integer_check == true) {
            for (int i = 0; i < row_count; i++) {
                int column_2_data = Integer.valueOf(t_merge_files.getValueAt(i, 2).toString());
                int column_3_data = Integer.valueOf(t_merge_files.getValueAt(i, 3).toString());
                if (column_3_data >= column_2_data) {
                    range_check = true;
                } else {
                    JOptionPane.showMessageDialog(null, LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_PAGE_RANGE_ERROR")
                            + " " + (i + 1) + " (" + column_2_data + " > " + column_3_data + ")",
                            LocalizationManager.getCurrent_resource().getString("ERROR"), 0);
                    LoggerManager.getLogger().log(Level.SEVERE, "Page range error in merge table. Message = {0}", column_2_data + ">" + column_3_data);
                    range_check = false;
                    break;
                }
            }
        }
        // greater than total check
        if (range_check == true) {
            for (int i = 0; i < row_count; i++) {
                int column_2_data = Integer.valueOf(t_merge_files.getValueAt(i, 2).toString());
                int column_3_data = Integer.valueOf(t_merge_files.getValueAt(i, 3).toString());
                int total_pages = Integer.valueOf(t_merge_files.getValueAt(i, 1).toString());
                if (column_2_data > total_pages || column_3_data > total_pages) {
                    // out of range
                    JOptionPane.showMessageDialog(null, LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_GREATER_PAGE_NUMBER")
                            + " " + (i + 1) + "\n" + LocalizationManager.getCurrent_resource().getString("MERGE_FILES_PANEL_TOTAL_PAGES_NOTE")
                            + " (1" + " ~ " + Integer.valueOf(t_merge_files.getValueAt(i, 1).toString()) + ")",
                            LocalizationManager.getCurrent_resource().getString("ERROR"), 0);
                    LoggerManager.getLogger().log(Level.SEVERE, "Page number is greater than the total pages.");
                    bigger_than_total_check = false;
                    break;
                } else {
                    bigger_than_total_check = true;
                }
            }
        }

        if (range_check == true && integer_check == true && bigger_than_total_check == true) {
            return true;
        } else {
            return false;
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_add_and_browse;
    private javax.swing.JButton bt_cancel;
    private javax.swing.JButton bt_info;
    private javax.swing.JButton bt_merge;
    private javax.swing.JButton bt_move_down;
    private javax.swing.JButton bt_move_up;
    private javax.swing.JButton bt_remove;
    private javax.swing.JButton bt_remove_all;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel l_author;
    private javax.swing.JLabel l_keywords;
    private javax.swing.JLabel l_subject;
    private javax.swing.JLabel l_title;
    private javax.swing.JPanel p_files_to_merge;
    private javax.swing.JPanel p_note;
    private javax.swing.JTable t_merge_files;
    private javax.swing.JTextArea ta_info;
    private javax.swing.JTextField tf_author;
    private javax.swing.JTextField tf_keywords;
    private javax.swing.JTextField tf_subject;
    private javax.swing.JTextField tf_title;
    // End of variables declaration//GEN-END:variables
}
